//=============================================================================

#include "../inc/aSystem.h"
#include "../inc/aAux.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

//=============================================================================

aSystem::aSystem()
{
	hWnd = 0;
	hRc = 0;
	hDc = 0;
	windowX = 1024/2 - 400;
	windowY = 768/2 - 300;
	windowWidth = 800;
	windowHeight = 600;
	nearClip = 0.1f;
	farClip = 1000.0f;
	fov = 45;
  frameRate = 0;
  motionRate = 1.0;
  framesInterval = 0;
  cameraSpeed = 0.15f;
  winRect = new tagRECT;
  fixedFps = 160;
}

//=============================================================================

bool aSystem::asInitOGL(HDC dc, LPRECT rect)
{
  if (!dc)
    return 0;

  hDc = dc;
  
  winRect->bottom = rect->bottom;
  winRect->left = rect->left;
  winRect->right = rect->right;
  winRect->top = rect->top;

  windowX = rect->left;
  windowY = rect->top;
	windowWidth = rect->right - rect->left;
  windowHeight = rect->bottom - rect->top;

  unsigned int pixelFormat;
  PIXELFORMATDESCRIPTOR pfd = 
	{
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		24,
		0, 0, 0, 0, 0, 0,
		0,
		0,
		0,
		0, 0, 0, 0,
		32,
		0,
		0,
		PFD_MAIN_PLANE,
		0,
		0, 0, 0
	};

  pixelFormat = ChoosePixelFormat(hDc, &pfd);
  SetPixelFormat(hDc, pixelFormat, &pfd);
  hRc = wglCreateContext(hDc);
  wglMakeCurrent(hDc, hRc);

  glClearColor(0, 0, 0, 0);
	glClearDepth(1);

	glPointSize(3);
	glLineWidth(1);

	glEnable(GL_TEXTURE_2D);
  glEnable(GL_POINT_SMOOTH);
  glEnable(GL_LINE_SMOOTH);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  //glEnable(GL_COLOR_MATERIAL);
  //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  glEnableClientState(GL_NORMAL_ARRAY);
	glShadeModel(GL_SMOOTH);
  //glViewport(0, 0, windowWidth, windowHeight);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

  camera.position.x = 3;
  camera.position.y = 3;
  camera.position.z = 3;
  camera.upVector.y = 1;

  glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);

  return 1;
}

//=============================================================================

void aSystem::asBeginToPaint()
{
	glLoadIdentity();
  gluPerspective(fov, float(winRect->right - winRect->left)/float(winRect->bottom - winRect->top), nearClip, farClip);
	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  gluLookAt(camera.position.x, camera.position.y, camera.position.z,
            camera.viewPoint.x, camera.viewPoint.y, camera.viewPoint.z,
            camera.upVector.x, camera.upVector.y, camera.upVector.z);

  //glDisable(GL_TEXTURE_2D);
  //glBegin(GL_LINES);
  //glColor3ub(255, 0, 0);
  //  glVertex3f(0, 0, 0);
  //  glVertex3f(1, 0, 0);
  //glColor3ub(0, 255, 0);
  //  glVertex3f(0, 0, 0);
  //  glVertex3f(0, 1, 0);
  //glColor3ub(0, 0, 255);
  //  glVertex3f(0, 0, 0);
  //  glVertex3f(0, 0, 1);
  //glEnd();
  //glEnable(GL_TEXTURE_2D);
}

//=============================================================================

void aSystem::asKeyPress()
{
}

//=============================================================================

void aSystem::asGetFrameRate()
{
  static float lastTimeR = 0.0;
  static float frameTimeR = 0.0;
  float currentTimeR = GetTickCount()*0.001f;
  
  framesInterval = currentTimeR - frameTimeR;
  frameTimeR = currentTimeR;
  frameRate++;

  if ((currentTimeR - lastTimeR) > 1.0)
  {
    lastTimeR = currentTimeR;
    frameRate = 0;
  }
  dt = framesInterval / motionRate;
}

//=============================================================================

void aSystem::asSetOrtho()
{
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  glOrtho(0, windowWidth, 0, windowHeight, -farClip, farClip);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

//=============================================================================

void aSystem::asSetPerspective()
{
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluPerspective(fov, float(winRect->right - winRect->left)/float(winRect->bottom - winRect->top), nearClip, farClip);
  gluLookAt(camera.position.x, camera.position.y, camera.position.z,
            camera.viewPoint.x, camera.viewPoint.y, camera.viewPoint.z,
            camera.upVector.x, camera.upVector.y, camera.upVector.z);
}

//=============================================================================

aSystem::~aSystem()
{
	if (hRc)
	{
		wglMakeCurrent(0, 0);
		wglDeleteContext(hRc);
	}
	if (hDc)
		ReleaseDC(hWnd, hDc);
  if (winRect)
    delete winRect;
}

//=============================================================================
